package com.spzx.test1.canguan;
/*
    并非全力
*/

import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomUtils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class CookRoom {
    Lock lock = new ReentrantLock();
    Condition cookLock = lock.newCondition();
    Condition cutLock = lock.newCondition();
    Condition giveLock = lock.newCondition();

    String foodStatus = "cut";


    @SneakyThrows
    public void cut() {
        lock.lock();
        try {
            while (!foodStatus.equals("cut")) {
                cutLock.await();
            }
            System.out.println(Thread.currentThread().getName()+"正在切菜");
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"菜切完了，唤醒炒菜线程");
            foodStatus = "cook";
            cookLock.signal();
        } finally {
            lock.unlock();
        }
    }

    @SneakyThrows
    public void cook() {
        lock.lock();
        try {
            while (!foodStatus.equals("cook")) {
                cookLock.await();
            }
            System.out.println(Thread.currentThread().getName()+"正在炒菜");
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"菜炒完了，唤醒端菜线程");
            foodStatus = "Lock";
            giveLock.signal();
        } finally {
            lock.unlock();
        }

    }

    @SneakyThrows
    public void Lock() {
        lock.lock();
        try {
            while (!foodStatus.equals("Lock")) {
                giveLock.await();
            }
            System.out.println(Thread.currentThread().getName()+"正在端菜");
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName()+"菜端完了，唤醒切菜线程");
            foodStatus = "cut";
            cutLock.signal();
        } finally {
            lock.unlock();
        }
    }


}
